package java_thinking.unit_18_IO._18_7;

import java_thinking.unit_18_IO._18_1.Directory;

import java.io.File;
import java.io.IOException;

/**
 * @Desc:   用 Directory.walk() 和 BinaryFile来验证在某个树下的所有.class文件是以十六进制字符“CAFEBABE”开头
 * @author: cww
 * @DateTime: 2019/5/16 8:58
 */

public class E20_ClassSignatureChecker {
    final static byte[] signature = {(byte)202, (byte)254, (byte)254, (byte)186, (byte)190};
    public static void main(String[] args) throws IOException {
        String dir = "src/maindemo/java/java_thinking/unit_18_IO/_18_7";
        System.out.println(Directory.walk(dir, ".*\\.class"));
        for (File file : Directory.walk(dir, ".*\\.class")) {
            byte[] bt = BinaryFile.read(file);
            for (int i = 0; i < signature.length; i++) {
                if (bt[i] != signature[i]) {
                    System.out.println(file + " is corrupt!");
                    break;
                } else {
                    System.out.println("match");
                }
            }
        }
    }
}
